<?php
/**
 * damn pdo can't be
 * unset by ref
 * so lets have a wrapper
 * and an overall control
 * of pdo refs!
 * @uses PDO
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.data
 * @copyright bbqq qstufie.com
 */
class Blib_Data_Dao_Wrapper_Pdo
{
    /**
     * the id
     * @var str
     */
    protected $_id = null;


    /**
     * pdo array to keep
     * @var array
     */
    protected static $_pdo = array();

    /**
     * override construct to have self registered
     * in static
     * @return void
     */
    public function __construct($dsn, $username = null, $password = null, $driver_options = array())
    {
        $this->_id = md5($dsn . $username . $password);
        // force singleton
        if (empty(self::$_pdo[$this->_id]) || !self::$_pdo[$this->_id] instanceof PDO) {
            self::$_pdo[$this->_id] = new PDO($dsn, $username, $password, $driver_options);
        }

    }// end _construct


    /**
     * pass all functions to pdo...
     * @return void
     */
    public function __call($name, $args)
    {
        // pass thru
        if (!empty(self::$_pdo[$this->_id])) {
            return call_user_func_array(array(self::$_pdo[$this->_id], $name), $args);
        }

        throw new exception('PDO object is not found.');

    } // end __call


    /**
     * purge all connections
     * @return void
     */
    public static function purgeAll()
    {
        foreach (self::$_pdo as &$pdo) {
            $pdo = null;
        }

        self::$_pdo = array();

    }// end purgeAll

}// end Blib_Data_Dao_Wrapper_Pdo
?>